<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        function f1(){
            var all=document.getElementById("all");//DOM对象
            var inputs=document.querySelectorAll("tbody input");//集合
            if(all.checked){
                for(var i=0;i<inputs.length;i++){
                    inputs[i].checked=true;
                }

            }else{
                for(var i=0;i<inputs.length;i++){
                    inputs[i].checked=false;
                }
            }
        }
        
        
       
        function f2(){
            var count=0;//个数
            var all=document.getElementById("all");//DOM对象
            var inputs=document.querySelectorAll("tbody input");

            //从头到尾重新查找被选中的个数
            for(var i=0;i<inputs.length;i++){
                if(inputs[i].checked){
                    count++;
                }
            }
            console.log(count);//1  2
            if(count==inputs.length){
                all.checked=true;
            }else{
                all.checked=false;
            }
        }
    </script>
</head>
<body>
    <table border="1" width="500">
        <thead>
            <tr>
                <th><input type="checkbox" id="all" onclick="f1()">全选</th>
                <th>th02</th>
                <th>th03</th>
                <th>th04</th>
            </tr>
        </thead>
        <tbody align="center">
            <tr>
                <td><input type="checkbox" onclick="f2()"></td>
                <td>td02</td>
                <td>td03</td>
                <td>td04</td>
            </tr>
            <tr>
                <td><input type="checkbox" onclick="f2()"></td>
                <td>td02</td>
                <td>td03</td>
                <td>td04</td>
            </tr>
            <tr>
                <td><input type="checkbox" onclick="f2()"></td>
                <td>td02</td>
                <td>td03</td>
                <td>td04</td>
            </tr>
            <tr>
                <td><input type="checkbox" onclick="f2()"></td>
                <td>td02</td>
                <td>td03</td>
                <td>td04</td>
            </tr>
        </tbody>
    </table>
</body>
</html>